package cn.edu.scau.cmi.xiaowenyang.client;

import java.util.List;

import cn.edu.scau.cmi.xiaowenyang.safeCompositePattern.Component;
import cn.edu.scau.cmi.xiaowenyang.safeCompositePattern.Composite;
import cn.edu.scau.cmi.xiaowenyang.safeCompositePattern.Leaf;
import javafx.application.Application;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.Pane;
import javafx.stage.Stage;

public class SafeCompositePatternClient extends Application {

	public static void main(String[] args) {
		launch(args);
	}

	private Composite root = new Composite("根");
	private int compositeNum = 0;
	private int leafNum = 0;

	@Override
	public void start(Stage primaryStage) throws Exception {
		primaryStage.setTitle("安全组合模式");
		TextArea textArea = new TextArea();
		Pane pane = new Pane();
		Button addComposite = new Button("添加树枝节点");
		Button addLeaf = new Button("添加叶子节点");
		Button clear = new Button("清空节点");
		Button display = new Button("显示节点");

		addComposite.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				try {
					root.add(new Composite("树枝" + compositeNum));
					textArea.setText("树枝节点" + (compositeNum++) + "添加成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		addLeaf.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				try {
					root.add(new Leaf("叶子" + leafNum));
					textArea.setText("叶子节点" + (leafNum++) + "添加成功");
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		clear.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				try {
					root.clear();
					textArea.setText("节点清空成功");
					compositeNum = leafNum = 0;
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		display.setOnMouseClicked(new EventHandler<MouseEvent>() {
			@Override
			public void handle(MouseEvent event) {
				// TODO Auto-generated method stub
				try {
					List<Component> list = root.getChildren();
					textArea.clear();
					for (Component component : list) {
						textArea.appendText(component.display() + "\n");
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		});

		addComposite.setLayoutX(550);
		addComposite.setLayoutY(100);

		addLeaf.setLayoutX(550);
		addLeaf.setLayoutY(150);

		clear.setLayoutX(550);
		clear.setLayoutY(200);

		display.setLayoutX(550);
		display.setLayoutY(250);

		textArea.setPrefHeight(300);
		textArea.setPrefWidth(500);
		textArea.setLayoutX(0);
		textArea.setLayoutY(0);
		textArea.setEditable(false);

		pane.getChildren().addAll(textArea, addComposite, addLeaf, clear, display);
		primaryStage.setScene(new Scene(pane, 700, 500));
		primaryStage.show();
	}
}
